package adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.PagerAdapter;
import android.text.SpannableString;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import com.example.lxh.testinfo.R;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.*;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import java.util.ArrayList;
import java.util.List;

import format.myAxisformat;

/**
 * Created by Lxh on 2017/12/27.
 */

public class PagerViewAdapter extends PagerAdapter implements
        SeekBar.OnSeekBarChangeListener, OnChartValueSelectedListener {
    private List<View> list;
    private LinearLayout m_picChart;
    private GraphicalView graphicalView;
    private Context context;
    protected HorizontalBarChart mChart;
    protected HorizontalBarChart mChart1;
    private com.github.mikephil.charting.charts.BarChart barChart;
    private String[] mMonths =
            new String[]{"5条以上违章", "3-5条违章", "1-2条违章"};
    private PieChart mPichart;
    private PieChart mPichart_1;
    private String[] xAxisMath = new String[]{
            "机动车逆行行驶", "违规使用专用车道", "违反禁令标志指示",
            "不按规定系安全带", "机动车不走机动车道", "违反信号灯规定",
            "违反禁止标线指示", "违规停车拒绝驶离", "违规驶入导向车道",
            "超速行驶"
    };

    public PagerViewAdapter(List<View> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @SuppressLint("WrongViewCast")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(list.get(position));
        View views = list.get(position);
        m_picChart = views.findViewById(R.id.picChart);
        switch (position) {
            case 0:
               /* // 给饼状图赋值
                double[] values = {412.0d, 1231.0d};
                CategorySeries mDaraset = getDataSet(values);
                //颜色初始化
                int[] colors = {Color.parseColor("#4572A7"), Color.parseColor("#AA4643")};
                DefaultRenderer mRefender = getRefeder(colors);
                //将画好的饼状图  添加到 布局当中
                graphicalView = ChartFactory.getPieChartView(views.getContext(), mDaraset, mRefender);
                m_picChart.addView(graphicalView);*/
                LayoutInflater inflater_1 = LayoutInflater.from(context);
                View pieChartView = inflater_1.inflate(R.layout.pic_dome_layout, null);
                mPichart_1 = pieChartView.findViewById(R.id.mPieChart);
                mPichart_1.setUsePercentValues(true);
                mPichart_1.getDescription().setEnabled(false);
                mPichart_1.setExtraOffsets(5, 10, 5, 5);
                mPichart_1.setDragDecelerationFrictionCoef(0.99f);
                mPichart_1.setCenterText(new SpannableString("有违规车辆和无违规车辆占比统计"));
                mPichart_1.setDrawHoleEnabled(false);
                mPichart_1.setHoleColor(Color.WHITE);

                mPichart_1.setTransparentCircleAlpha(110);
                mPichart_1.setTransparentCircleColor(Color.WHITE);
                mPichart_1.setHoleRadius(58f);
                mPichart_1.setTransparentCircleRadius(61f);
                mPichart_1.setDrawCenterText(true);
                mPichart_1.setRotationEnabled(true);
                mPichart_1.setHighlightPerTapEnabled(true);
                mPichart_1.setOnChartValueSelectedListener(this);
                ArrayList<PieEntry> entries = new ArrayList<>();
                entries.add(new PieEntry(21.8f, "无违规"));
                entries.add(new PieEntry(72.1f, "有违规"));
                setDataPieChart_1(entries);
                mPichart_1.animateY(1400, Easing.EasingOption.EaseInOutQuad);

                Legend legends = mPichart_1.getLegend();
                legends.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
                legends.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
                legends.setOrientation(Legend.LegendOrientation.VERTICAL);
                legends.setDrawInside(false);
                legends.setXEntrySpace(7f);
                legends.setYEntrySpace(0f);
                legends.setYOffset(0f);
                mPichart_1.setEntryLabelColor(Color.WHITE);
                mPichart_1.setEntryLabelTextSize(12f);
                m_picChart.addView(pieChartView);
                break;
            case 1:
                LayoutInflater inflaters = LayoutInflater.from(context);
                View picView = inflaters.inflate(R.layout.pic_dome_layout, null);
                mPichart = picView.findViewById(R.id.mPieChart);
                mPichart.setUsePercentValues(true);
                mPichart.getDescription().setEnabled(false);
                mPichart.setExtraOffsets(5, 10, 5, 5);
                mPichart.setDragDecelerationFrictionCoef(0.95f);
                mPichart.setCenterText(new SpannableString("有无重复违章记录的车辆的占比统计"));
                mPichart.setDrawHoleEnabled(false);
                mPichart.setHoleColor(Color.WHITE);

                mPichart.setTransparentCircleAlpha(110);
                mPichart.setTransparentCircleColor(Color.WHITE);
                mPichart.setHoleRadius(58f);
                mPichart.setTransparentCircleRadius(61f);
                mPichart.setDrawCenterText(true);
                mPichart.setRotationEnabled(true);
                mPichart.setHighlightPerTapEnabled(true);
                mPichart.setOnChartValueSelectedListener(this);
                ArrayList<PieEntry> entries1 = new ArrayList<>();
                entries1.add(new PieEntry(13.8f, "无重复违规"));
                entries1.add(new PieEntry(86.1f, "有重复违规"));
                setDataPieChart(entries1);
                mPichart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

                Legend legends1 = mPichart.getLegend();
                legends1.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
                legends1.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
                legends1.setOrientation(Legend.LegendOrientation.VERTICAL);
                legends1.setDrawInside(false);
                legends1.setXEntrySpace(7f);
                legends1.setYEntrySpace(0f);
                legends1.setYOffset(0f);
                mPichart.setEntryLabelColor(Color.WHITE);
                mPichart.setEntryLabelTextSize(12f);
                m_picChart.addView(picView);
                 /* double[] values1 = {13.8d, 81.6d};
                CategorySeries mDaraset1 = getDataSet1(values1);
                int[] colors1 = {Color.parseColor("#4572A7"), Color.parseColor("#AA4643")};
                DefaultRenderer mRefender1 = getRefeder1(colors1);
                graphicalView = ChartFactory.getPieChartView(views.getContext(), mDaraset1, mRefender1);
                m_picChart.addView(graphicalView);*/
                break;
            case 2:
                LayoutInflater inflater = LayoutInflater.from(context);
                View Viewinflater = inflater.inflate(R.layout.horzrtlayout, null);
                mChart = Viewinflater.findViewById(R.id.chart1);
                mChart.setOnChartValueSelectedListener(this);
                mChart.setDrawBarShadow(false);
                mChart.setDrawValueAboveBar(true);
                mChart.getDescription().setEnabled(false);
                mChart.setMaxVisibleValueCount(60);
                mChart.setPinchZoom(false);
                mChart.setDrawGridBackground(false);
                mChart.setDoubleTapToZoomEnabled(false);//禁止双击放大
                mChart.setPinchZoom(true);//禁止xy轴放大

                XAxis x1 = mChart.getXAxis();
                x1.setPosition(XAxis.XAxisPosition.BOTTOM);
                x1.setDrawAxisLine(true);
                x1.setDrawGridLines(false);
                x1.setGranularity(10f);
                x1.setValueFormatter(new IAxisValueFormatter() {
                    @Override
                    public String getFormattedValue(float value, AxisBase axis) {
                        Log.w("Lxh", "222: " + value);
                        return mMonths[(int) value % mMonths.length];
                    }
                });
                YAxis y1 = mChart.getAxisLeft();
                y1.setDrawAxisLine(true);
                y1.setDrawGridLines(false);
                y1.setAxisMinimum(0f);
                y1.setValueFormatter(new myAxisformat());
                YAxis y2 = mChart.getAxisRight();
                y2.setDrawAxisLine(true);
                y2.setDrawGridLines(false);
                y2.setAxisMinimum(0f);
                y2.setValueFormatter(new myAxisformat());

                setData(3, 50);
                mChart.setFitBars(true);
                mChart.animateY(2500);
                Legend legend = mChart.getLegend();
                legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
                legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
                legend.setDrawInside(false);
                legend.setFormSize(8f);
                legend.setEnabled(false);
                legend.setXEntrySpace(4f);
                m_picChart.addView(Viewinflater);
                break;
            case 3:
                String[] titles = new String[]{"无违规", "有违规"};
                List<double[]> vs = new ArrayList<>();
                vs.add(new double[]{431d, 200d, 400d, 788d, 456d});
                vs.add(new double[]{300d, 123d, 234d, 411d, 234d});
                XYMultipleSeriesDataset dataset = builBarDataset(titles, vs);
                int[] colorss = new int[]{Color.parseColor("#EB7208"), Color.parseColor("#6A9800")};
                XYMultipleSeriesRenderer renderer = builBarRender(colorss);
                renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
                renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
                graphicalView = ChartFactory.
                        getBarChartView(views.getContext(), dataset, renderer, BarChart.Type.STACKED);
                m_picChart.addView(graphicalView);
                break;
            case 4:
                String[] titles_1 = new String[]{"无违规", "有违规"};
                List<double[]> vs_1 = new ArrayList<>();
                vs_1.add(new double[]{16300d, 18300d});
                vs_1.add(new double[]{15431d, 16431d});
                XYMultipleSeriesDataset dataset_1 = builBarDataset_1(titles_1, vs_1);
                int[] colorss_1 = new int[]{Color.parseColor("#EB7208"), Color.parseColor("#6A9800")};
                XYMultipleSeriesRenderer renderer_1 = builBarRender_1(colorss_1);
                renderer_1.getSeriesRendererAt(0).setDisplayChartValues(true);
                renderer_1.getSeriesRendererAt(1).setDisplayChartValues(true);
                graphicalView = ChartFactory.
                        getBarChartView(views.getContext(), dataset_1, renderer_1, BarChart.Type.STACKED);
                m_picChart.addView(graphicalView);
                break;
            case 5:
                LayoutInflater inflater1 = LayoutInflater.from(context);
                View hist_View = inflater1.inflate(R.layout.histgramelayout, null);
                barChart = hist_View.findViewById(R.id.chart_barchart);
                barChart.getDescription().setEnabled(false);
                barChart.setDrawValueAboveBar(true);
                barChart.setOnChartValueSelectedListener(this);
                barChart.setMaxVisibleValueCount(60);
                barChart.setPinchZoom(false);
                barChart.setDrawBarShadow(false);

                XAxis xAxis = barChart.getXAxis();
                xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
                xAxis.setDrawGridLines(false);
                xAxis.setDrawAxisLine(true);
                xAxis.setGranularity(10f);
                xAxis.setTextSize(20);
                xAxis.setValueFormatter(new myAxisformat());

                YAxis yAxis = barChart.getAxisLeft();
                yAxis.setDrawAxisLine(true);
                yAxis.setDrawGridLines(false);
                yAxis.setAxisMinimum(0f);
                yAxis.setValueFormatter(new myAxisformat());

                setData_barChart(12, 50);
                barChart.setFitBars(true);
                barChart.getAxisLeft().setDrawGridLines(false);
                barChart.animateY(2500);

                Legend legend1 = barChart.getLegend();
                legend1.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
                legend1.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
                legend1.setDrawInside(false);
                legend1.setFormSize(8f);
                legend1.setXEntrySpace(4f);
                barChart.getLegend().setEnabled(false);
                m_picChart.addView(hist_View);
                break;
            case 6:
                LayoutInflater inflater2 = LayoutInflater.from(context);
                View barchart_View = inflater2.inflate(R.layout.horzrt_1_layout, null);
                mChart1 = barchart_View.findViewById(R.id.chart2);
                mChart1.setOnChartValueSelectedListener(this);
                mChart1.setDrawBarShadow(false);
                mChart1.setDrawValueAboveBar(true);
                mChart1.getDescription().setEnabled(false);
                mChart1.setMaxVisibleValueCount(60);
                mChart1.setPinchZoom(true);
                mChart1.setDrawGridBackground(false);

                final XAxis xAxis1 = mChart1.getXAxis();
                xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
                xAxis1.setDrawAxisLine(true);
                xAxis1.setDrawGridLines(false);
                xAxis1.setGranularity(10f);
                xAxis1.setValueFormatter(new IAxisValueFormatter() {
                    @Override
                    public String getFormattedValue(float value, AxisBase axis) {
                        Log.w("Lxh", value + "getFormattedValue: " + value % xAxisMath.length);
                        return xAxisMath[(int) value % xAxisMath.length];
                    }
                });

                YAxis yAxis1 = mChart1.getAxisLeft();
                yAxis1.setDrawAxisLine(true);
                yAxis1.setDrawGridLines(false);
                yAxis1.setAxisMinimum(0f);
                yAxis1.setValueFormatter(new myAxisformat());

                YAxis yAxis2 = mChart1.getAxisRight();
                yAxis2.setDrawAxisLine(true);
                yAxis2.setDrawGridLines(false);
                yAxis2.setAxisMinimum(0f);
                yAxis2.setValueFormatter(new myAxisformat());

                setData1(12, 50);
                mChart1.setFitBars(true);
                mChart1.animateY(2500);
                Legend legend2 = mChart1.getLegend();
                legend2.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
                legend2.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
                legend2.setDrawInside(false);
                legend2.setFormSize(8f);
                legend2.setEnabled(false);
                legend2.setXEntrySpace(4f);
                m_picChart.addView(barchart_View);
                break;
        }
        return list.get(position);
    }

    private void setDataPieChart_1(ArrayList<PieEntry> entries) {
        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);
        ArrayList<Integer> Colorss = new ArrayList<>();
        Colorss.add(Color.parseColor("#4572A7"));
        Colorss.add(Color.parseColor("#AA4643"));
        Colorss.add(ColorTemplate.getHoloBlue());
        dataSet.setColors(Colorss);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(15f);
        data.setValueTextColor(Color.WHITE);
        mPichart_1.setData(data);
        mPichart_1.highlightValue(null);
        mPichart_1.invalidate();
    }


    private void setDataPieChart(ArrayList<PieEntry> entries) {
        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);
        ArrayList<Integer> colors = new ArrayList<>();
        //int[] colors = {Color.parseColor("#4572A7"), Color.parseColor("#AA4643")};
        colors.add(Color.parseColor("#4572A7"));
        colors.add(Color.parseColor("#AA4643"));
        colors.add(ColorTemplate.getHoloBlue());
        dataSet.setColors(colors);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(15f);
        data.setValueTextColor(Color.WHITE);
        mPichart.setData(data);
        mPichart.highlightValue(null);
        mPichart.invalidate();
    }

    public void setData1(int count, int range) {
        float barWidth = 9f;
        float spaceForBar = 10f;
        ArrayList<BarEntry> yVals = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * range);
            yVals.add(new BarEntry(i * spaceForBar, val));
        }
        BarDataSet set;
        if (mChart1.getData() != null && mChart1.getData().getDataSetCount() > 0) {
            set = (BarDataSet) mChart1.getData().getDataSetByIndex(0);
            set.setValues(yVals);
            mChart1.getData().notifyDataChanged();
            mChart1.notifyDataSetChanged();
        } else {
            set = new BarDataSet(yVals, "data");
            set.setDrawIcons(false);
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set);
            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setBarWidth(barWidth);
            mChart1.setData(data);
        }
        List<Integer> listColor = new ArrayList<>();
        listColor.add(Color.parseColor("#48687E"));//48687E
        listColor.add(Color.parseColor("#2A0F18"));//2A0F18
        listColor.add(Color.parseColor("#070760"));//070760
        listColor.add(Color.parseColor("#4F622A"));//4F622A
        listColor.add(Color.parseColor("#B8B0DA"));//B8B0DA
        listColor.add(Color.parseColor("#769339"));//769339
        listColor.add(Color.parseColor("#983439"));//983439
        listColor.add(Color.parseColor("#FDC701"));//FDC701
        listColor.add(Color.parseColor("#4F7FBA"));//4F7FBA
        listColor.add(Color.parseColor("#E86A0F"));//E86A0F
        listColor.add(Color.parseColor("#973377"));//973377
        listColor.add(Color.parseColor("#381716"));//381716
        set.setColors(listColor);
    }


    public void setData(int count, int range) {
        float barWidth = 9f;
        float spaceForBar = 10f;
        ArrayList<BarEntry> yVals = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * range);
            yVals.add(new BarEntry(i * spaceForBar, val));
        }
        BarDataSet set;
        if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
            set = (BarDataSet) mChart.getData().getDataSetByIndex(0);
            set.setValues(yVals);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            set = new BarDataSet(yVals, "data");
            set.setDrawIcons(false);
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set);
            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setBarWidth(barWidth);
            mChart.setData(data);
        }

        List<Integer> listColor = new ArrayList<>();
        listColor.add(Color.RED);
        listColor.add(Color.BLUE);
        listColor.add(Color.GREEN);
        set.setColors(listColor);
    }

    public void setData_barChart(int count, int range) {
        float barWidth = 9f;
        float spaceForBar = 10f;
        ArrayList<BarEntry> yVales = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * range);
            yVales.add(new BarEntry(i * spaceForBar, val));
        }
        BarDataSet set;
        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            set = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            set.setValues(yVales);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            set = new BarDataSet(yVales, "data");
            set.setDrawIcons(false);
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set);
            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setBarWidth(barWidth);
            barChart.setData(data);
        }
        List<Integer> listColor = new ArrayList<>();
        listColor.add(Color.parseColor("#48687E"));//48687E
        listColor.add(Color.parseColor("#2A0F18"));//2A0F18
        listColor.add(Color.parseColor("#070760"));//070760
        listColor.add(Color.parseColor("#4F622A"));//4F622A
        listColor.add(Color.parseColor("#B8B0DA"));//B8B0DA
        listColor.add(Color.parseColor("#769339"));//769339
        listColor.add(Color.parseColor("#983439"));//983439
        listColor.add(Color.parseColor("#FDC701"));//FDC701
        listColor.add(Color.parseColor("#4F7FBA"));//4F7FBA
        listColor.add(Color.parseColor("#E86A0F"));//E86A0F
        listColor.add(Color.parseColor("#973377"));//973377
        listColor.add(Color.parseColor("#381716"));//381716
        set.setColors(listColor);
        barChart.invalidate();
    }


    private XYMultipleSeriesDataset builBarDataset_1(String[] titles, List<double[]> values) {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        for (int i = 0; i < titles.length; i++) {
            CategorySeries series = new CategorySeries(titles[i]);
            double[] v = values.get(i);
            int seriesLenght = v.length;
            Log.w("Lxh", "builBarDataset_1SSS: " + seriesLenght);
            for (int k = 0; k < seriesLenght; k++) {
                Log.w("Lxh", "builBarDataset_1: " + v[k]);
                series.add(v[k]);
            }
            dataset.addSeries(series.toXYSeries());
        }
        return dataset;
    }

    private XYMultipleSeriesRenderer builBarRender_1(int[] colors) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(30);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setChartTitle("男性和女性有无车辆违章的占比统计表");
        renderer.setYLabels(9);
        renderer.setShowGridX(true);
        renderer.setMargins(new int[]{50, 80, 15, 10});
        renderer.setGridColor(Color.BLACK);
        renderer.setXLabels(0);
        renderer.addXTextLabel(1, "女性");
        renderer.addXTextLabel(2, "男性");
        renderer.setXAxisMin(0.5);
        renderer.setXAxisMax(5.5);
        renderer.setYAxisMin(13000);
        renderer.setYAxisMax(20000);

        renderer.setDisplayValues(true);
        renderer.setAxesColor(Color.BLACK);
        renderer.setLabelsColor(Color.BLACK);
        renderer.setMarginsColor(Color.WHITE);
        renderer.setXLabelsAlign(Paint.Align.RIGHT);
        renderer.setYLabelsAlign(Paint.Align.RIGHT);

        renderer.setPanEnabled(false);
        renderer.setZoomRate(1.0f);
        renderer.setBarSpacing(0.2f);

        for (int color : colors) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(color);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }


    private XYMultipleSeriesDataset builBarDataset(String[] titles, List<double[]> values) {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        int lenght = titles.length;
        for (int i = 0; i < lenght; i++) {
            CategorySeries series = new CategorySeries(titles[i]);
            double[] v = values.get(i);
            int seriesLenght = v.length;
            Log.w("Lxh", "builBarDataset:SSSS>> " + seriesLenght);
            for (int k = 0; k < seriesLenght; k++) {
                Log.w("Lxh", "builBarDataset:>> " + v[k]);
                series.add(v[k]);
            }
            dataset.addSeries(series.toXYSeries());
        }
        return dataset;
    }

    private XYMultipleSeriesRenderer builBarRender(int[] colors) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(30);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setChartTitle("年龄群体车辆违章的占比统计表");
        renderer.setXLabels(0);
        renderer.setYLabels(6);
        renderer.setShowGridX(true);
        renderer.setGridColor(Color.BLACK);
        renderer.setMargins(new int[]{50, 80, 15, 10});
        renderer.addXTextLabel(1, "90后");
        renderer.addXTextLabel(2, "80后");
        renderer.addXTextLabel(3, "70后");
        renderer.addXTextLabel(4, "60后");
        renderer.addXTextLabel(5, "50后");
        renderer.setXAxisMin(0.5);
        renderer.setXAxisMax(7.5);
        renderer.setYAxisMin(1);
        renderer.setYAxisMax(1200);
        renderer.setAxesColor(Color.GRAY);
        renderer.setMarginsColor(Color.WHITE);

        renderer.setXLabelsAlign(Paint.Align.RIGHT);
        renderer.setYLabelsAlign(Paint.Align.RIGHT);
        renderer.setPanEnabled(false);
        renderer.setZoomRate(1.5f);
        renderer.setBarSpacing(0.5f);
        renderer.setLabelsColor(Color.BLACK);
        for (int color : colors) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(color);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }


    public CategorySeries getDataSet(double[] values) {
        CategorySeries series = new CategorySeries("饼状图");
        series.add("有违规", values[0]);
        series.add("无违规", values[1]);
        return series;
    }

    public DefaultRenderer getRefeder(int[] colors) {
        DefaultRenderer refeder = new DefaultRenderer();
        refeder.setLegendTextSize(20);
        refeder.setZoomEnabled(false);
        refeder.setChartTitleTextSize(30);
        refeder.setChartTitle("有违章车辆和无违章车辆的占比统计");

        refeder.setLabelsTextSize(20);
        refeder.setFitLegend(false);
        refeder.setPanEnabled(false);//设置是否可以平移
        refeder.setDisplayValues(true);
        refeder.setMargins(new int[]{20, 30, 25, 0});
        refeder.setBackgroundColor(Color.WHITE);
        refeder.setLabelsColor(Color.BLACK);

        for (int color : colors) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(color);
            refeder.addSeriesRenderer(r);
        }
        return refeder;
    }

    public CategorySeries getDataSet1(double[] values) {
        CategorySeries series = new CategorySeries("饼状图");
        series.add("有重复违规", values[0]);
        series.add("无重复违规", values[1]);
        return series;
    }

    public DefaultRenderer getRefeder1(int[] colors) {
        DefaultRenderer refeder = new DefaultRenderer();
        refeder.setLegendTextSize(20);
        refeder.setZoomEnabled(false);
        refeder.setChartTitleTextSize(30);
        refeder.setChartTitle("有无重复违章记录的车辆的占比统计");
        refeder.setLabelsTextSize(20);
        refeder.setFitLegend(false);
        refeder.setPanEnabled(false);//设置是否可以平移
        refeder.setDisplayValues(true);
        refeder.setMargins(new int[]{20, 30, 25, 0});
        refeder.setBackgroundColor(Color.WHITE);
        refeder.setLabelsColor(Color.BLACK);
        for (int color : colors) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(color);
            refeder.addSeriesRenderer(r);
        }
        return refeder;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onValueSelected(Entry e, Highlight h) {
    }

    @Override
    public void onNothingSelected() {
    }
}

